import {
    Component,
    Injector,
    ViewEncapsulation,
    ViewChild,
    ElementRef
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    PipelineListDto,
    PipelineServiceProxy
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import * as moment from 'moment';

@Component({
    selector: 'rw21',
    templateUrl: './rw21.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class RW21ModalComponent extends AppComponentBase {
    saving = false;

    pipe: PipelineListDto = new PipelineListDto();

    splitQuatily = 1;

    dueDate = new Date();

    constructor(
        injector: Injector,
        private _taskServiceProxy: PipelineServiceProxy
    ) {
        super(injector);
    }

    load(pipe: PipelineListDto): void {
        this.pipe = pipe;
    }

    save(): void {
        this.saving = true;

        this._taskServiceProxy
            .splitPipeline(
                this.pipe.id,
                this.pipe.taskId,
                moment(this.dueDate),
                this.splitQuatily
            )
            .pipe(
                finalize(() => {
                    this.saving = false;
                })
            )
            .subscribe(result => {
                this.notify.success('拆分成功');
            });
    }
}
